package com.dems.service.impl;

import com.dems.dao.dataobject.CfgDeviceSignal;
import com.dems.dao.dataobject.CfgSignal;
import com.dems.dao.dataobject.DeviceSignalData;
import com.dems.dao.mapper.la.CfgDeviceSignalMapper;
import com.dems.dao.mapper.la.CfgSignalMapper;
import com.dems.dao.mapper.la.DeviceSignalDataMapper;
import com.dems.domain.DeviceSignalDataDto;
import com.dems.domain.DeviceSignalReqVO;
import com.dems.domain.signal.SignalDataDto;
import com.dems.service.DeviceSignalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class DeviceSignalServiceImpl implements DeviceSignalService {
    @Autowired
    private CfgDeviceSignalMapper cfgDeviceSignalMapper;

    @Autowired
    private DeviceSignalDataMapper deviceSignalDataMapper;

    @Autowired
    private CfgSignalMapper cfgSignalMapper;

    @Override
    public List<CfgDeviceSignal> selectByRoomId(DeviceSignalReqVO deviceSignalReqVO) {
        return cfgDeviceSignalMapper.selectByRoomId(Long.valueOf(deviceSignalReqVO.getRoomId()));
    }

    @Override
    public List<SignalDataDto> selectByDeviceId(DeviceSignalReqVO deviceSignalReqVO) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<CfgDeviceSignal> deviceSignals = cfgDeviceSignalMapper.selectByDeviceId(deviceSignalReqVO.getDeviceId());
        List<SignalDataDto> signalDataDtos = new ArrayList<>();
        for(CfgDeviceSignal deviceSignal : deviceSignals){
            DeviceSignalData deviceSignalData = new DeviceSignalData();
            deviceSignalData.setDeviceId(deviceSignal.getDeviceId());
            deviceSignalData.setSignalId(deviceSignal.getSignalId());
            DeviceSignalData devSig = deviceSignalDataMapper.selectLastSignalDataList(deviceSignalData);
            CfgSignal cfgSignal = cfgSignalMapper.selectByPrimaryKey(deviceSignal.getSignalId());
            SignalDataDto signalDataDto = new SignalDataDto();
            BeanUtils.copyProperties(cfgSignal, signalDataDto);
            signalDataDto.setSignalId(deviceSignal.getSignalId().toString());
            if(devSig != null) {
                signalDataDto.setCollectValue(devSig.getCollectValue());
                signalDataDto.setCollectTime(simpleDateFormat.format(devSig.getCollectTime()));
            }
            signalDataDtos.add(signalDataDto);
        }
        return signalDataDtos;
    }

    @Override
    public List<DeviceSignalData> selectSignalData(DeviceSignalDataDto deviceSignalDataDto) {
        DeviceSignalData deviceSignalData = new DeviceSignalData();
        BeanUtils.copyProperties(deviceSignalDataDto, deviceSignalData);
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(deviceSignalDataDto.getEndDate() == null){
            deviceSignalData.setEndDate(simpleDateFormat.format(calendar.getTime()));
        }
        if(deviceSignalDataDto.getStartDate() == null){
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            deviceSignalData.setStartDate(simpleDateFormat.format(calendar.getTime()));
        }
        return deviceSignalDataMapper.selectSignalDataList(deviceSignalData);
    }

    @Override
    public int deleteDevSignal(DeviceSignalDataDto deviceSignalDataDto) {
        CfgDeviceSignal cfgDeviceSignal = new CfgDeviceSignal();
        cfgDeviceSignal.setDeviceId(deviceSignalDataDto.getDeviceId());
        cfgDeviceSignal.setSignalId(deviceSignalDataDto.getSignalId());
        cfgDeviceSignal.setIsDelete(deviceSignalDataDto.getIsDelete());
        return cfgDeviceSignalMapper.updateByPrimaryKeySelective(cfgDeviceSignal);
    }
}
